package com.lyk.coursearrange.util;

import cn.hutool.crypto.asymmetric.RSA;

import com.lyk.coursearrange.dao.JurisdictionDao;
import com.lyk.coursearrange.entity.Admin;
import com.lyk.coursearrange.entity.dto.AdminDto;
import com.lyk.coursearrange.entity.dto.JurisdictionDto;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class JwtToken {


    @Autowired
    private JurisdictionDao jurisdictionDao;



    private  String RSA_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC3gB8jKsdd3zzs9LM7ijU6PfX/RUxPGAj7OLGS1oNXFp0/xUa1gLTf663X2DgBB9zZ+JViReWEoNqk881jqAzeha6OjX2Ulo7VMIL16QYPxcHisUivgGVz8Haj/UBl6XGFL0T2eUcxGap4RkU2WVJyQSUM2aqO7+YutwBPUC7btw9ecFUJS5MvzEWtR5TVfWzjNCZcHaxwkjsOlTyH4SP52nEXylTZokMii+6Xr9kZt6nDQ8qaHir1VyUifoEAkYuhd/xCBung6VcErp+/0zEXYXQqj/6Id8MQSXaY9GfLcZt5pczs4IZ1L/2Jo3uM1cRIIR5vllBgoEntkOzzXouLAgMBAAECggEAemiHXOg+kkpG1DLwFTm/BBktLppFq0V3SE6rdi6O8tttDotE/fLbtbv7/c6InXdnRihjTmkeEHlVIVBZpK8H1rEKnqGYMjS1F1YDJpPJrV/YVKa6vAhEpTC1k/NusA0n4xaDRH/mk8RxSUZnCThQnVMDqizRcARsYqFFHaN5fqre5JvkCyQ1e58q5EHkf481HzikPBZ8kmXDHvbjGxdI9rwEFeouGtjtLWUePHENhFZkSePCBUwWwo3Ls8Qs9jYfnESIfXpqif9P1fO00iXJK7gaAiKVx7jHPbnNIbkncLad9n9aIuJ7YhZhmjKUtih/V69Y7N1ldWR5r1TJGuEXYQKBgQDsJUE3l8IFiuhR3uDjw8wH3cMPtFKi7UOCu3l+ieXg4DhgKD1yXleefJ4Gr4Zun/8SjldIMyGucF5Mmh9N0dkas1U8rwBEiBPoiEErD0lkLrWM2wboZqFO6nr0KkfoGbhZnRgnBllQB1SML4efOBcFIhovMhYGn15BAxmtNSEAiQKBgQDG7b8FatY/bKv7AByNzyl9w9XzUVvMVsdZuis5aD+oEG8R34EOWLmh1eXHwvhwYbCTjRZR1y5acnb58q1bkqvOOJubFNp+ZHPLX6fAMao1vZ/jpMSLvIrW0D+mBq+5bqzo0yQu1z27y6TGkpeKKMv4x4YI+A/tQ+M2fHUpbFFecwKBgEVa67yYJbPbCECDZSl/5/yndRK3s02Gwb0zsosnlQxmzifvqZxdeP5jO0ZbZvA2rYOVqqzZ15qUBh4ZZfhFGa5t3Srlev6luLMi0YEEDFoQHeIwseavvdCfrVzozhWs+CfZq65KfxCyFnLVws9QefJYAbi6zR1E0Tnm3zOv1vGBAoGBAIwdj2uwacH4uFYuKO6VmSL1WnBswNz3zqhbofBohlXJZYJQTCWfxvi/oX6HzNfTZz02ph71ErgwYa8CCKs0bwDtDGU6Vf+qn4kDEjQkh0AdFU6RksLeBWjy+7Hkicdxtiaz+Ytonhn1gVVAka6Ft82+M6GDoCw9of+RwDByFAoTAoGBALps2pAov/PbflL2lgM9HHbxqBW452NQAUdkE/ATSGv5CnrTWd0av2O6UKMWXB68VrRqbJC/4y3a5heohLl+SZpDDlriM+n3uu5bxiC5QssRRisM2+1sGIB0Ec2YD83WLUnLzThbsC2+nmRsC2DmdP3smcF/3p20Lyo6C4bszBmS";


    public AdminDto MsgLogin(Admin user){
        RSA rsa = new RSA(RSA_PRIVATE_KEY,null);
        Date date = new Date();
        JwtBuilder jwtBuilder = Jwts.builder();
        jwtBuilder.claim("username",user.getRealname())//设置私有声明
                .setIssuedAt(date)//起始时间
                .setExpiration(new Date(date.getTime()+1000*60*10))//过期时间
                .setId(user.getId().toString())//设置jti，它是JWT的唯一标识
                .signWith(rsa.getPrivateKey(), SignatureAlgorithm.RS256);//生产签名信息*/
        String  token =jwtBuilder.compact();
        System.out.println("============================================="+user.getId());
        List<JurisdictionDto>  jurisdictionDtoList= jurisdictionDao.selectJurByUserId(user.getId());
         return AdminDto.builder()
                .id(user.getId())
                .adminNo(user.getAdminNo())
                .realname(user.getRealname())
                .userType(user.getUserType())
                .jur(jurisdictionDtoList)
                 .sourcepeopleId(user.getSourcepeopleId())
                .createTime(user.getCreateTime())
                .updateTime(user.getUpdateTime())
           /*     .token(token)
                .expTime(1000 * 60 * 10)
                .genTime(date.getTime())*/
                .build();
    }
}
